package com.visec.hdy.hadoop.inquiry;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.visec.hdy.hadoop.standard.FSB;

public class FileInquiryImpl extends DefaultInquiryImpl<String, File> {

	public FileInquiryImpl(FSB fsb) {
		super(fsb);
	}

	@Override
	public void queryAll() {
		List<File> listAll = this.fsb.listAll(new File(context.getFolder()));
		context.setQueryFile(listAll);
	}

	@Override
	public void query(String start, String end) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<File> next(int i, Class<File> c) {
		List<File> files = new ArrayList<>();
		int length = i;
		while (length > 0) {
			long read = context.read(length);
			if (read == -1)
				break;
			length -= read;
			files.add(context.getFile());
		}
		return files;
	}

	@Override
	public void queryLike(String like) {
		List<File> sortFiles = this.fsb.listAll(new File(context.getFolder()));
		List<File> files = new ArrayList<>();
		for (int i = 0; i < sortFiles.size(); i++) {
			String name = sortFiles.get(i).getName();
			if (name.indexOf(like) >= 0) {
				files.add(sortFiles.get(i));
			}
		}
		context.setQueryFile(files);
	}

}
